package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.AnProduct;
import com.animal.entity.AnUser;
import com.animal.mapper.AnProductMapper;
import com.animal.response.Result;
import com.animal.utils.UpPhotoNameUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-01-28
 */
@RestController
@RequestMapping("/an-product")
public class AnProductController extends JSONAuthentication {
    @Autowired
    private  AnProductMapper anProductMapper;

    @ApiOperation(value = "添加商品")
    @PostMapping("/add")
    public void addProduct(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String  name,@RequestParam("price") float price,@RequestParam("represent") String represent) throws Exception{
        AnProduct anProduct=new AnProduct();
        anProduct.setName(name);
        anProduct.setPrice(price);
        anProduct.setRepresent(represent);

        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
        Path path = Paths.get("D:\\自己的项目\\流浪动物救助\\adopt\\adopt\\src\\assets\\picture\\" + fileName);
        //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
        Files.write(path, bytes);//写入文件
        String avatar_url=fileName;
        anProduct.setPrPicture(avatar_url);
        response.reset();
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anProduct.setPrCreatTime(timestamp);
        int i=anProductMapper.insert(anProduct);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
        }
    @ApiOperation(value = "删除商品")
    @PostMapping("/del")
    public void delProduct(HttpServletRequest request, HttpServletResponse response, @RequestParam("prId") int prId ) throws Exception{
        int i=anProductMapper.deleteById(prId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改商品")
    @PostMapping("/upd")
    public void updProduct(HttpServletRequest request, HttpServletResponse response, @RequestBody AnProduct anProduct)throws Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anProduct.setPrUpdTime(timestamp);
        int i=anProductMapper.updateById(anProduct);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询商品")
    @PostMapping("/look")
    public void lookProduct(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{
        Page<AnProduct> userPage = new Page<>(current,size);

        Page<AnProduct> userPages=  anProductMapper.selectPage(userPage,null);
        Result ok = Result.ok().data("product",userPages);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "按名字模糊查询商品")
    @PostMapping("/findByname")
    public void findByname(HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String name)throws  Exception{
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("name",name);
        List<AnProduct> list =anProductMapper.selectList(wrapper);
        if(list.size()>0){
            Result ok = Result.ok().data("product",list);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有此商品");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "按商品id查询商品")
    @PostMapping("/findByid")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("prId") int  prId)throws  Exception{
      AnProduct list=  anProductMapper.selectById(prId);

        if(list!=null){
            Result ok = Result.ok().data("product",list);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有此商品");
        this.WriteJSON(request, response, error);
    }

}

